package com.kdtech.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class ObjectUtils {

	/**
	 * 压缩一个对象 这个对象必须 可序列化
	 *
	 * @param entity
	 * @return
	 */
	public static byte[] ZipObject(Object entity) {
		byte[] objbytes = null;
		try {
			objbytes = ObjectUtils.zip(object2Bytes(entity));
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			// e1.printStackTrace();
		}
		return objbytes;
	}
	/**
	 * 压缩一堆对象
	 *
	 * @param objbytes
	 * @return
	 */
	public static Collection<byte[]> ZipObjectList(Collection<Object> objs) {
		Collection<byte[]> objbytes = new ArrayList<byte[]>();
		for (Object oneobj : objs) {
			try {
				byte[] onebyte = ObjectUtils.zip(object2Bytes(oneobj));
				objbytes.add(onebyte);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				// e.printStackTrace();
			}
		}
		return objbytes;
	}
	
	/**
	 * 从对象获取一个字节数组
	 */
	private static byte[] object2Bytes(Object obj) throws Exception {
		if (obj == null) {
			return null;
		}
		ByteArrayOutputStream bo = new ByteArrayOutputStream();
		ObjectOutputStream oo = new ObjectOutputStream(bo);
		oo.writeObject(obj);
		return bo.toByteArray();
	}
	
	public static byte[] zip(byte[] b) throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		GZIPOutputStream gos = new GZIPOutputStream(baos);
		ByteArrayInputStream bais = new ByteArrayInputStream(b);
		IOUtils.CopyInputStreamToOutputStream(bais, gos);
		gos.finish();
		gos.flush();

		bais.close();
		gos.close();
		return baos.toByteArray();
	}
	
	/**
	 * 解压缩一个对象
	 *
	 * @param objbytes
	 * @return
	 */
	public static Object UnZipObject(byte[] objbytes) {
		Object obj = null;
		try {
			obj = (Object) bytes2Object(ObjectUtils.unZip(objbytes));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
		}
		return obj;
	}

	/**
	 * 解压 一堆对象
	 *
	 * @param objbytes
	 * @return
	 */
	public static Collection<Object> UnZipObjectList(Collection<byte[]> objbytes) {
		Collection<Object> obj = new ArrayList<Object>();
		;
		for (byte[] objectbyte : objbytes) {
			try {
				Object oneobj = (Object) bytes2Object(ObjectUtils.unZip(objectbyte));
				obj.add(oneobj);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				// e.printStackTrace();
			}
		}
		return obj;
	}
	
	/**
	 * 从字节数组获取对象
	 */
	private static Object bytes2Object(byte[] objBytes) throws Exception {
		if (objBytes == null || objBytes.length == 0) {
			return null;
		}
		ByteArrayInputStream bi = new ByteArrayInputStream(objBytes);
		ObjectInputStream oi = new ObjectInputStream(bi);
		return oi.readObject();
	}
	public static byte[] unZip(byte[] b) throws IOException {
		GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(b));
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		IOUtils.CopyInputStreamToOutputStream(gis, baos);
		gis.close();
		return baos.toByteArray();
	}
}
